package net.lab1024.sa.admin.module.upstream.box;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.device.terminal.domain.DeviceTerminalEntity;
import net.lab1024.sa.admin.module.device.terminal.domain.DeviceTerminalVO;
import net.lab1024.sa.admin.module.system.login.domain.RequestEmployee;
import net.lab1024.sa.admin.module.upstream.box.domain.UpstreamBoxQueryForm;
import net.lab1024.sa.admin.module.upstream.box.domain.UpstreamBoxEntity;
import net.lab1024.sa.admin.module.upstream.box.domain.UpstreamBoxVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.enumeration.EmployeeTypeEnum;
import net.lab1024.sa.base.common.enumeration.ReviewStatusEnum;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class UpstreamBoxService {

    private final UpstreamBoxDao upstreamBoxDao;

    private final SqlSessionFactory sqlSessionFactory;

    public UpstreamBoxService(UpstreamBoxDao upstreamBoxDao,
                              SqlSessionFactory sqlSessionFactory) {
        this.upstreamBoxDao = upstreamBoxDao;
        this.sqlSessionFactory = sqlSessionFactory;
    }

    /**
     * 根据上报的结果保存上游节点信息
     */
    public void saveByDeployResult(DeviceTerminalEntity deviceTerminalEntity) {
        // 保存上游节点所需信息
        UpstreamBoxEntity entity = UpstreamBoxEntity.builder()
                .upstreamId(deviceTerminalEntity.getUpstreamId())
                .upstreamCode(deviceTerminalEntity.getUpstreamCode())
                .userId(deviceTerminalEntity.getUserId())
                .enterpriseId(deviceTerminalEntity.getEnterpriseId())
                .ip(deviceTerminalEntity.getIp())
                .isp(deviceTerminalEntity.getIsp())
                .province(deviceTerminalEntity.getProvince())
                .city(deviceTerminalEntity.getCity())
                .cpuCores(deviceTerminalEntity.getCpuCores())
                .memorySize(deviceTerminalEntity.getMemorySize())
                .diskSize(deviceTerminalEntity.getDiskSize())
                .reviewStatus(ReviewStatusEnum.PASS.getValue())
                .reviewTime(LocalDateTime.now())
                .build();
        upstreamBoxDao.insert(entity);
    }

    public List<UpstreamBoxEntity> queryByOffsetAndCriteria(Integer begin,
                                                            Integer offset,
                                                            UpstreamBoxQueryForm queryForm) {
        // 页数不能小于1
        return upstreamBoxDao.queryByOffsetAndForm(begin, offset, queryForm);
    }

    // @Async 先不加异步，后面量起来后测试异步开销没问题后在开启
    public void updateBatch(List<UpstreamBoxEntity> entities) {
        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
            UpstreamBoxDao mapper = session.getMapper(UpstreamBoxDao.class);
            for (UpstreamBoxEntity entity : entities) {
                mapper.updateById(entity);
            }
            session.commit();
        } catch (Exception e) {
            log.error("批量更新上游盒子设备信息时出错", e);
        }
    }

    public PageResult<UpstreamBoxVO> queryByPage(UpstreamBoxQueryForm queryForm, RequestEmployee requestUser) {
        Integer employeeType = requestUser.getEmployeeType();
        if (EmployeeTypeEnum.ENTERPRISE.getValue().equals(employeeType)) {
            queryForm.setEnterpriseId(requestUser.getEnterpriseId());
        }
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<UpstreamBoxVO> list = upstreamBoxDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }
}
